<?php
class item_uploademotion extends template_item
{
	var $default_name = "Upload An Emotion";
	var $default_description = "Upload an emotion for people to use.";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['max_size'] = 1500;
			$this->item_config['clickable'] = 0;
			$this->item_config['overwrite'] = 0;
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Make newer emotions clickable?</b>",
															$this->ipsclass->adskin->form_yes_no("item[".$this->item['item_id']."][clickable]", $this->item_config['clickable']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Overwrite older emotions?</b><br />This option will allow overwriting existing image files and/or existing emotion text.",
															$this->ipsclass->adskin->form_yes_no("item[".$this->item['item_id']."][overwrite]", $this->item_config['overwrite']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Maximum size for the emotions to be uploaded?</b><br />In bytes. Input 0 for no limit.",
															$this->ipsclass->adskin->form_simple_input("item[".$this->item['item_id']."][max_size]", $this->item_config['max_size']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['clickable'] = $input['clickable'] & 1;
		$save['overwrite'] = $input['overwrite'] & 1;
		$save['max_size'] = intval($input['max_size']);
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_ucp');
		$this->ipsclass->load_language('lang_market_items');
		
		$upload_form = "";
		foreach($inventory_ids as $inv_id)
		{
			$upload_form .= <<<EOF
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['select_an_image_file']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='file' size='30' name='upload_emotion_{$inv_id}' />
			</td>
		</tr>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['text_for_emotion']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='text' size='10' name='emotion_text_{$inv_id}' />
			</td>
		</tr>
EOF;
		}
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['upload_emotions']}</div>
<form action='{$this->base_url}' method='post' enctype='multipart/form-data'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left' colspan='2'>
				{$this->ipsclass->lang['emotion_limit']}
			</td>
		</tr>
{$upload_form}
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['upload_emotions']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done($inventory_ids = array())
	{
		foreach($inventory_ids as $inv_id)
		{
			if(!isset($_FILES['upload_emotion_'.$inv_id]) || !is_uploaded_file($_FILES['upload_emotion_'.$inv_id]['tmp_name']) || !isset($this->ipsclass->input['emotion_text_'.$inv_id]))
			{
				return false;
			}
		}
		return isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$emodir = $this->ipsclass->market_class->get_dir_list(CACHE_PATH.'style_emoticons',0);
		
		require_once(KERNEL_PATH."class_upload.php");
		$upload = new class_upload();
		
		$delete = $inventory_ids;
		
		require_once( ROOT_PATH.'sources/action_admin/emoticons.php' );
		$emoticons           = new ad_emoticons();
		$emoticons->ipsclass =& $this->ipsclass;
		
		foreach($inventory_ids as $i => $inv_id)
		{
			$this->ipsclass->input['emotion_text_'.$inv_id] = str_replace( '&quot;', "", $this->ipsclass->input['emotion_text_'.$inv_id]);
			
			if(!$this->ipsclass->input['emotion_text_'.$inv_id])
			{
				unset($delete[$i]);
				continue;
			}
			
			$upload->error_no = 0;
			$upload->upload_form_field = 'upload_emotion_'.$inv_id;
			$upload->out_file_dir = $this->ipsclass->vars['upload_dir'];
			$upload->max_file_size = $this->item_config['max_size'];
			$upload->allowed_file_ext = $emoticons->allowed_files;
			$upload->out_file_name = "emo-".md5(microtime());
			
			$upload->upload_process();
			
			if($upload->error_no)
			{
				unset($delete[$i]);
				continue;
			}
			
			foreach($emodir as $dir)
			{
				$folder = CACHE_PATH.'style_emoticons/'.$dir;
				if(!file_exists($folder."/".$upload->original_file_name) || $this->item_config['overwrite'])
				{
					if ( @copy( $upload->saved_upload_name, $folder."/".$upload->original_file_name ) )
					{
						@chmod( $folder."/".$upload->original_file_name, 0777 );
					}
				}
				
				if($old_emo = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'emoticons','where'=>"emo_set='".$dir."' AND (typed='".$this->ipsclass->input['emotion_text_'.$inv_id]."' OR image='".$upload->original_file_name."')")))
				{
					if($this->item_config['overwrite'])
					{
						if($old_emo['typed'] == $this->ipsclass->input['emotion_text_'.$inv_id])
						{
							$this->ipsclass->DB->do_update( 'emoticons', array( 'clickable' => $old_emo['clickable'], 'image' => $upload->original_file_name ), "emo_set='".$dir."' AND typed='".$this->ipsclass->input['emotion_text_'.$inv_id]."'" );
						}
						else
						{
							$this->ipsclass->DB->do_update( 'emoticons', array( 'clickable' => $old_emo['clickable'], 'typed' => $this->ipsclass->input['emotion_text_'.$inv_id] ), "emo_set='".$dir."' AND image='".$upload->original_file_name."'" );
						}
					}
				}
				else
				{
					$this->ipsclass->DB->do_insert( 'emoticons', array( 'clickable' => $this->item_config['clickable'], 'typed' => $this->ipsclass->input['emotion_text_'.$inv_id], 'image' => $upload->original_file_name, 'emo_set' => $dir ) );
				}
			}
			
			@unlink($upload->saved_upload_name);
			$this->_write_log($inv_id, "Uploaded emotion, ".$upload->original_file_name);
		}
		
		$emoticons->emoticon_rebuildcache();
		
		$this->delete_items($delete);
		
		$this->ipsclass->market_class->redirect('emotions_uploaded');
	}
}
?>